package com.financing.platform.web.api;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ProjectActivitiesDTO;
import com.financing.platform.service.IFpProjectActivitiesService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.DateUtil;
import com.financing.platform.util.StringUtil;

@Controller("fpProjectActivitiesAction")
@RequestMapping("/api/projectActivities")
public class FpProjectActivitiesAction extends BaseAction {
	
	@Autowired
	IFpProjectActivitiesService fpProjectActivitiesService;

	/***
	 * 
	
	* @Title: addProjectActivities
	
	* @Description: 添加产品活动
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/addProjectActivities.shtml")
	@ResponseBody
	public ResultMap addProjectActivities(HttpServletRequest request,
			HttpServletResponse response, ProjectActivitiesDTO projectActivitiesDTO) {
		ResultMap resultMap = new ResultMap();
		try {
			log.info("Begin add info !");

			DTOdateSet.setCreateInfo(projectActivitiesDTO, request.getSession());
			DTOdateSet.setUpdateInfo(projectActivitiesDTO, request.getSession());
			
			//活动参数校验
			validateProjectActivities(projectActivitiesDTO, resultMap);
			
			if(StringUtils.isNotBlank(resultMap.getStatusCode())){
				return resultMap;
			}
			
			//查询code是否存在
			//该活动存不存在    
			ProjectActivitiesDTO oldDto=new ProjectActivitiesDTO();
			oldDto.setActivitiesCode(projectActivitiesDTO.getActivitiesCode());
			List<ProjectActivitiesDTO> queryProjectActivities = fpProjectActivitiesService.queryProjectActivities(oldDto);
			if(queryProjectActivities!=null&&queryProjectActivities.size()>0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_ACTIVITIES_CODE_EXIST);
				return resultMap;
			}
			
			
			//新增
			try {
				fpProjectActivitiesService.addFpProjectActivities(projectActivitiesDTO);
			} catch (Exception e) {
				log.error(" addProjectActivities SQL error!", e);
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_ACTIVITIES_CODE_EXIST);
				return resultMap;
			}
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			

			log.info("End addProjectActivities info !");
		} catch (Exception e) {
			log.error(" addProjectActivities info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}
	
	/***
	 * 
	
	* @Title: updateProjectActivities
	
	* @Description: 编辑产品活动
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/updateProjectActivities.shtml")
	@ResponseBody
	public ResultMap updateProjectActivities(HttpServletRequest request,
			HttpServletResponse response, ProjectActivitiesDTO projectActivitiesDTO) {
		ResultMap resultMap = new ResultMap();
		try {
			log.info("Begin updateProjectActivities info !");

			DTOdateSet.setUpdateInfo(projectActivitiesDTO, request.getSession());
			
			Integer id=projectActivitiesDTO.getId();
			if(id==null||id==0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			//该活动存不存在
			ProjectActivitiesDTO oldDto=new ProjectActivitiesDTO();
			oldDto.setId(id);
			List<ProjectActivitiesDTO> queryProjectActivities = fpProjectActivitiesService.queryProjectActivities(oldDto);
			if(queryProjectActivities==null||queryProjectActivities.size()==0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_ACTIVITIES_NOT_EXIST);
				return resultMap;
			}
			
			//活动参数校验
			validateProjectActivities(projectActivitiesDTO, resultMap);
			
			if(StringUtils.isNotBlank(resultMap.getStatusCode())){
				return resultMap;
			}
			
			fpProjectActivitiesService.updateProjectActivities(projectActivitiesDTO);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			

			log.info("End updateProjectActivities info !");
		} catch (Exception e) {
			log.error(" updateProjectActivities info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	/***
	 * 
	
	* @Title: validateProjectActivities
	
	* @Description:校验产品活动
	
	* @param @param projectActivitiesDTO
	* @param @param resultMap
	* @param @throws ParseException    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public void validateProjectActivities(
			ProjectActivitiesDTO projectActivitiesDTO, ResultMap resultMap)
			throws ParseException {
		String activitiesName=projectActivitiesDTO.getActivitiesName();//活动名称
		String activitiesDesc=projectActivitiesDTO.getActivitiesDesc();//活动说明
		String activitiesSkipName=projectActivitiesDTO.getActivitiesSkipName();//活动专题跳转名称
		String activitiesSkipLink=projectActivitiesDTO.getActivitiesSkipLink();//活动专题跳转链接
		String activitiesStatus=projectActivitiesDTO.getActivitiesStatus();
		String activitiesCode=projectActivitiesDTO.getActivitiesCode();//code
		
		String activitiesStartDateStr=projectActivitiesDTO.getActivitiesStartDateStr();
		String activitiesEndDateStr=projectActivitiesDTO.getActivitiesEndDateStr();
		
		//产品活动名称和说明,描述,活动编码不能为空
		boolean nameFlagEmpty=StringUtil.isNullOrEmptyNStr(activitiesName,activitiesDesc,activitiesStatus,activitiesCode);
		// 时间校验
		boolean timeEmpty=StringUtil.isNullOrEmptyNStr(activitiesStartDateStr,activitiesEndDateStr);
		
		if(nameFlagEmpty||timeEmpty){
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			return ;
		}
		
		//名称和链接  必须是同时存在
		boolean nameFlagEmp=StringUtil.isNullOrEmptyNStr(activitiesSkipName);
		boolean linFlagEmpty=StringUtil.isNullOrEmptyNStr(activitiesSkipLink);
		
		if(nameFlagEmp!=linFlagEmpty){
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			return ;
		}
		if(activitiesSkipName.length()>50){
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.STATUS_CODE_ACTIVITIES_NAME_ERROR);
			return ;
		}
		
		Date activitiesStartDate=DateUtil.parseString(activitiesStartDateStr);
		Date activitiesEndDate=DateUtil.parseString(activitiesEndDateStr);
		
		//开始时间要等于小于结束时间
		int cn=activitiesStartDate.compareTo(activitiesEndDate);
		if(cn>0){
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.STATUS_CODE_TIME_ERROR);
			return ;
		}
		
		projectActivitiesDTO.setActivitiesStartDate(activitiesStartDate);
		projectActivitiesDTO.setActivitiesEndDate(activitiesEndDate);
	}

	
	/***
	 * 
	
	* @Title: queryProjectActivities
	
	* @Description: 查询产品活动
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/queryProjectActivities.shtml")
	@ResponseBody
	public ResultMap queryProjectActivities(HttpServletRequest request,
			HttpServletResponse response, ProjectActivitiesDTO projectActivitiesDTO,Pager pager) {
		ResultMap resultMap = new ResultMap();
		try {
			
			log.info("Begin queryProjectActivities info !");
			projectActivitiesDTO.setBegin(pager.getStartIndex());
			projectActivitiesDTO.setPageSize(pager.getPageSize());
			
			int count = fpProjectActivitiesService.queryProjectActivitiesCount(projectActivitiesDTO);
			pager.setTotalCount(count);
			
			List<ProjectActivitiesDTO> queryProjectActivities = fpProjectActivitiesService.queryProjectActivities(projectActivitiesDTO);
			
			resultMap.setAttribute("list",queryProjectActivities);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			log.info("End queryProjectActivities info !");
		} catch (Exception e) {
			log.error(" queryProjectActivities info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}
	
	/***
	 * 
	
	* @Title: detailProjectActivities
	
	* @Description: 查询产品活动详情
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/detailProjectActivities.shtml")
	@ResponseBody
	public ResultMap detailProjectActivities(HttpServletRequest request,
			HttpServletResponse response, ProjectActivitiesDTO projectActivitiesDTO) {
		ResultMap resultMap = new ResultMap();
		try {
			
			log.info("Begin detailProjectActivities info !");
			
			Integer id=projectActivitiesDTO.getId();
			if(id==null||id==0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			//该活动存不存在
			ProjectActivitiesDTO oldDto=new ProjectActivitiesDTO();
			oldDto.setId(id);
			List<ProjectActivitiesDTO> queryProjectActivities = fpProjectActivitiesService.queryProjectActivities(oldDto);
			if(queryProjectActivities==null||queryProjectActivities.size()==0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_ACTIVITIES_NOT_EXIST);
				return resultMap;
			}
			
			resultMap.setAttribute("projectActivities",queryProjectActivities.get(0));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			log.info("End detailProjectActivities info !");
		} catch (Exception e) {
			log.error(" detailProjectActivities info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}
	
	/***
	 * 
	
	* @Title: queryProjectActivities
	
	* @Description: 查询产品活动列表     仅名称
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/queryProjectActivitiesNameList.shtml")
	@ResponseBody
	public ResultMap queryProjectActivitiesNameList(HttpServletRequest request,
			HttpServletResponse response,String isExpired,String isStatus) {
		ResultMap resultMap = new ResultMap();
		try {
			log.info("Begin queryProjectActivitiesNameList info !");
			
			
			ProjectActivitiesDTO projectActivitiesDTO = new ProjectActivitiesDTO();
			
			if(StringUtils.isNotBlank(isExpired) && "01".equals(isExpired)){
				projectActivitiesDTO.setIsExpired(isExpired);
			}
			
			if(StringUtils.isNotBlank(isStatus) && "01".equals(isStatus)){
				projectActivitiesDTO.setActivitiesStatus("01");
			}
			
			//查询所有活动
			List<ProjectActivitiesDTO> queryProjectActivities = fpProjectActivitiesService.queryProjectActivities(projectActivitiesDTO);
			
			resultMap.setAttribute("list",queryProjectActivities);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			log.info("End queryProjectActivitiesNameList info !");
		} catch (Exception e) {
			log.error(" queryProjectActivitiesNameList info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}
	
	
}
